package com.springboot.service;

import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.support.atomic.RedisAtomicInteger;
import org.springframework.stereotype.Component;

/**
 * redis服务
 *
 * @author supu
 * @create 2019-01-22 14:18
 **/
@Log4j2
@Component
public class RedisService {
    @Autowired
    private StringRedisTemplate redisTemplate;

    public int getIndex(String key){
        RedisAtomicInteger redisAtomicInteger = new RedisAtomicInteger(key, redisTemplate.getConnectionFactory());
        if(redisAtomicInteger.intValue() >= 20){
            redisAtomicInteger.set(0);
        }
        return redisAtomicInteger.incrementAndGet();
    }
}
